﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class EditUser : Form
    {
        private bool bEditing = false;
        private string sEditingUser = "";

        public EditUser()
        {
            InitializeComponent();
        }

        public EditUser(string userName)
        {
            sEditingUser = userName;
            InitializeComponent();
            AcceptButton = buttonOK;
        }

        private void buttonGO_Click(object sender, EventArgs e)
        {
            UserAccount user = null;
            try
            {
                user = UserAccountServices.GetUserAccountByUserName(textBoxUserName.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (user == null)
            {
                MessageBox.Show("Invalid Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EnableControls(true);

            if (user.UserType == 1)
            {
                comboBoxUserType.Text = "Administrator";
            }
            else if (user.UserType == 2)
            {
                comboBoxUserType.Text = "Normal User";
            }
            else if (user.UserType == 3)
            {
                comboBoxUserType.Text = "Advisor";
            }
            else if (user.UserType == 4)
            {
                comboBoxUserType.Text = "Client";
            }

            bEditing = true;
            sEditingUser = textBoxUserName.Text;
        }

        private void EditUser_Load(object sender, EventArgs e)
        {
            if (sEditingUser != "")
            {
                buttonGO.Hide();
                textBoxUserName.Text = sEditingUser;
                textBoxUserName.Enabled = false;
                bEditing = true;
                buttonGO_Click(null, null);
                return;
            }

            try
            {
	            AutoCompleteStringCollection data = new AutoCompleteStringCollection();
	
	            UserAccountCollection userCollection = UserAccountServices.GetUserAccountCollection();
	
	            for (int iI = 0; iI < userCollection.Count; iI++)
	            {
	                data.Add(userCollection[iI].UserName);
	            }
	
	            textBoxUserName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
	            textBoxUserName.AutoCompleteSource = AutoCompleteSource.CustomSource;
	            textBoxUserName.AutoCompleteCustomSource = data;
            }
            catch (System.Exception ex)
            {
            	
            }

            EnableControls(false);
        }

        private void EnableControls(bool bEnable)
        {
            textBoxNewPass.Enabled = bEnable;
            textBoxNewPass.Enabled = bEnable;
            textBoxConfirmPass.Enabled = bEnable;
            comboBoxUserType.Enabled = bEnable;
            buttonOK.Enabled = bEnable;
        }

        private void textBoxUaerName_TextChanged(object sender, EventArgs e)
        {
            if (bEditing)
            {
                bEditing = false;
                ClearFields();
                EnableControls(false);
            }
        }

        private void ClearFields()
        {
            textBoxNewPass.Text = "";
            textBoxNewPass.Text = "";
            textBoxConfirmPass.Text = "";
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            UserAccount actualAccount = null;
            try
            {
                actualAccount = UserAccountServices.GetUserAccountByUserName(textBoxUserName.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (actualAccount == null)
            {
                MessageBox.Show("Invalid Username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            UserAccount account = new UserAccount();
            account.UserName = textBoxUserName.Text;
            if (textBoxNewPass.Text == "")
            {
                account.Password = actualAccount.Password;
            }
            else
            {
                account.Password = MD5Calculator.CalculateMD5Hash(textBoxNewPass.Text);
            }

            if (comboBoxUserType.Text == "Administrator")
            {
                account.UserType = 1;
            }
            else if (comboBoxUserType.Text == "Normal User")
            {
                account.UserType = 2;
            }
            else if (comboBoxUserType.Text == "Advisor")
            {
                account.UserType = 3;
            }
            else if (comboBoxUserType.Text == "Client")
            {
                account.UserType = 4;
            }

            try
            {
                UserAccountServices.Update(account);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to edit account\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("User updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private bool validateInputs()
        {
            if (textBoxUserName.Text == "")
            {
                MessageBox.Show("Please enter required details", "Required Fields not Filled", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }

            if (textBoxNewPass.Text == "" && textBoxConfirmPass.Text != "")
            {
                MessageBox.Show("Please enter new password", "Required Fields not Filled", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }

            if (textBoxNewPass.Text != "" && textBoxConfirmPass.Text == "")
            {
                MessageBox.Show("Please enter password confirmation", "Required Fields not Filled", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }

            if (textBoxConfirmPass.Text != textBoxNewPass.Text)
            {
                MessageBox.Show("Confirmation password does not match the password", "Confirm Password Wrong", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }

            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void textBoxUserName_Enter(object sender, EventArgs e)
        {
            AcceptButton = buttonGO;
        }

        private void textBoxUserName_Leave(object sender, EventArgs e)
        {
            AcceptButton = buttonOK;
        }
    }
}
